package com.menhai.personmanagement.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.SysRole;
import com.menhai.personmanagement.entity.SysUser;
import com.menhai.personmanagement.entity.User;
import com.menhai.personmanagement.mapper.SysUserMapper;
import com.menhai.personmanagement.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author shikun
 * @since 2021-12-09
 */
 @Service
public class ISysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private UserMapper userMapper;

    public Page selectAll(int pagesize, int size){
        Page page = new Page(pagesize, size);
        return sysUserMapper.selectPage(page,null);
    }

    public int addSysUser(SysUser sysUser) throws Exception {
        if(this.findUserByAccount(sysUser) > 0){
            throw new Exception("账号已被注册");
        }
        return sysUserMapper.insert(sysUser);
    }

    /**
     * 查看账号是否被注册
     * @return
     */
    public int findUserByAccount(SysUser sysUser){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("account", sysUser.getAccount());
        return sysUserMapper.selectCount(queryWrapper);
    }

    public int editSysUser(SysUser sysUser){
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("jobnumber",sysUser.getAccount());
        List<User> userList = userMapper.selectList(queryWrapper);
        userList.forEach(item->{
            userMapper.updateById(User.builder().id(item.getId()).isAvailable(String.valueOf(sysUser.getIsAvailable())).build());
        });
        return sysUserMapper.updateById(sysUser);
    }

    public Page findSysUser(String name,String zhen,String cun,int pagesize,int size){
        Page page = new Page(pagesize, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(null != name && !name.equals("")) {
            queryWrapper.like("name", name);
        }
        if(null != cun && !cun.equals("")){
            queryWrapper.eq("hosnum",cun);
        }else if(null != zhen && !zhen.equals("")){
            queryWrapper.likeRight("hosnum",zhen);
        }
        queryWrapper.orderByAsc("createTime");
        return sysUserMapper.selectPage(page,queryWrapper);
    }

    public int delSysUser(int id){
        return sysUserMapper.deleteById(id);
    }

    public List login(String account, String password){
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("telephone", account);
//        queryWrapper.eq("password", password);
//        queryWrapper.eq("isAvailable", 1);
//
//
//        return sysUserMapper.selectOne(queryWrapper);
        return userMapper.loginActive(account,password,1);
    }
    public List<SysRole> findUserById(int id){
        return sysUserMapper.findUserRole(id);
    }

    public int editPass(String account, String pass){

        return sysUserMapper.editPass(account,pass);
    }
}
